package com.supreme.secretserver.demo;

import com.alibaba.fastjson.JSONObject;
import com.supreme.secretserver.entity.RsaKeyPair;
import com.supreme.secretserver.utils.RSAUtils;

import java.security.*;

public class RsaDemo {

    public static String privateKey =
            "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC8HXEdIRcyGPI9" +
            "4ULAoyEg/C/rdnukmtRWvoRTiyAZRqFVU1khL/2FrByNCL+ICw2LKxoHo9wQwv69" +
            "j01XEoKOcv4BOFSarxpGMA6WZsV9CMrk21OfPu0ffCWymmhhYZRdFzH5Rp42NlKr" +
            "LGUEQw5ABlF7D+RGqmfGVsWeEEj0TA9mKS9MOP3n7xYNX0pxFxfAcUlHiFGVHUgd" +
            "ry+zt20aq6vKJ2HB0h9rXAbIXsPOzNlRkV0/KJYeSwkIyBag4e5l72P/qBUWB327" +
            "pjYb1diKpAjYXu4w2cxIxqsbyrU0VgKmuMxnn4p5pV2XKqeXNA7LSeZiM9PYXXwa" +
            "Ou75X3pNAgMBAAECggEAFuEx18wliRdVp0cVml588gkaPvZLNYsLVn2XlJ2wSsK3" +
            "bqxTg+DVYgO7+T9eJErL2+hjIxi/9F3zI9UhH/0FWhQMZtOWHrY7aNwBejOHw5xl" +
            "KQ6wDgdWSeUMpLt5BJcct3HRxzLanVdgGKeupaUOP6VFFcWqCxpAwvPc10u9w9Ci" +
            "dwbyZat40e9h7Z8XMVyOz6mJknT+PCgX6Mlyyiqsp7Kfmzf11eC/AHeVP0OYvfOb" +
            "/04jJyEUDGJnB7OmtscJarnh7d3Wz8d8fWG3Urf0RgmqN944ualY4Ns/UPSPITJx" +
            "nz9XR3EQGvGl3IT/ltZarC5zbzp56ZhNMV7yMMvRQQKBgQD+9miusNa8qw29hKqU" +
            "ZEJEUzzOy2+PN00m7m90g78vdgaffLi06E3BIc/s2cpkvND8cLsd3w9DW3CnYfrk" +
            "KSne6TVRsdY2IV/ufFWF1NjZm3PYHUcZ/Ck8oHvKPZ83BiNaCAmZpms87M7CsRxq" +
            "t176rfL69AyddAtPK557laRokQKBgQC84WYUvX7jDzRGWyHRC6arRiPmG2h7ap3t" +
            "9nSj+sUd/xuUey4WW3k6T1+pTfhp8Kr2kvt85uJVkpFNo8K+9pY8VFCUgxOG+Ayf" +
            "DYczGwtJs50/32cSvYDZ1vxZIyjz9Y7nINp5avXyiDB5G1ddBzV5xyrN1ElIkUbx" +
            "C6PRybtz/QKBgHYAnZqchgZ0NLvONxTPAkyVKcOcLFnggnjOSg9nGolg2tfd5a71" +
            "BWepuo4ojp/pM+BiXHgK0QmfvJf7pR8MXjCpRuA0qqwK7ld2fC6gwGyWohsYEdxY" +
            "9Mz2idSkiJfy41G7/pV6jI96GofJaRmLHQ5wi9DSOzR0jPdrfMI+FAXBAoGANWvq" +
            "22LdSIs8Vcngvqd09GxaKvMFkiKNmIhkZRzAM8O7oTtVxa0JlJxuigwrfos7T5Iy" +
            "3xjOWzWhMzxmEGzeASI3yElqdHoj17iTzLiRcL8EctRU6s/+aZ6MsQlow9doG6cH" +
            "I+BEv9wVGavKAd3QGiliyeSI9aVhUzvfka5k/mECgYBFTETarXR62YG0rvq3FJDL" +
            "a5IUHcuKFxI2rpBIa1Rm9G4wuvDa8BYNdzHrZiDpz446pIRoPCFMtbBt/4Bp9so1" +
            "7r+CEAWl3p030shuaeG/LlTW0xV2gqFZ1KUtTKjbwapEqcasUb59C3XxQHeYW5lT" +
            "JWzRKoJfIWqyX3uGzmfF/g==";

    public static String publicKey =
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvB1xHSEXMhjyPeFCwKMh" +
            "IPwv63Z7pJrUVr6EU4sgGUahVVNZIS/9hawcjQi/iAsNiysaB6PcEML+vY9NVxKC" +
            "jnL+AThUmq8aRjAOlmbFfQjK5NtTnz7tH3wlsppoYWGUXRcx+UaeNjZSqyxlBEMO" +
            "QAZRew/kRqpnxlbFnhBI9EwPZikvTDj95+8WDV9KcRcXwHFJR4hRlR1IHa8vs7dt" +
            "GquryidhwdIfa1wGyF7DzszZUZFdPyiWHksJCMgWoOHuZe9j/6gVFgd9u6Y2G9XY" +
            "iqQI2F7uMNnMSMarG8q1NFYCprjMZ5+KeaVdlyqnlzQOy0nmYjPT2F18Gjru+V96" +
            "TQIDAQAB";

    public static void main(String[] args) throws Exception {
        System.out.println("RsaDemo");

        RsaKeyPair rsaKeyPair = null;
        try {
            rsaKeyPair = RSAUtils.generateKeyPair(512);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

//        final RsaKeyPair rsaKeyPair = new RsaKeyPair();
//        rsaKeyPair.setPrivateKey(privateKey);
//        rsaKeyPair.setPublicKey(publicKey);

        System.out.println("公钥：");
        System.out.println(rsaKeyPair.getPublicKey());
        System.out.println("私钥：");
        System.out.println(rsaKeyPair.getPrivateKey());
        String phone = "4008123123";

        //公钥加密，私钥解密
        String phoneRsa = RSAUtils.encryptByPublicKey(rsaKeyPair.getPublicKey(), phone);
        System.out.println("公钥加密后："+phoneRsa);
        String phoneRsa2 = RSAUtils.decryptByPrivateKey(rsaKeyPair.getPrivateKey(), phoneRsa);
        System.out.println("私钥解密后："+phoneRsa2);

        //私钥加密，公钥解密
        String phoneRsa3 = RSAUtils.encryptByPrivateKey(rsaKeyPair.getPrivateKey(), phone);
        System.out.println("私钥加密后："+phoneRsa3);
        String phoneRsa4 = RSAUtils.decryptByPublicKey(rsaKeyPair.getPublicKey(), phoneRsa3);
        System.out.println("公钥解密后："+phoneRsa4);

        final JSONObject jsonData = new JSONObject() {{
            put("message", "hello world");
            put("status", 123456);
            put("data", "8uhb2wsx");
        }};
        final String sign = RSAUtils.privateKeySign(jsonData.toString().getBytes(), privateKey);
        System.out.println("sign = " + sign);

        jsonData.put("data", "9ijn8uhb7ygv");
        final boolean verify = RSAUtils.publicKeyVerify(jsonData.toString().getBytes(), publicKey, sign);
        System.out.println("verify = " + verify);
    }
}
